from dataclasses import dataclass
import json
from typing import Dict, List, Tuple, Optional
from datetime import datetime, timedelta

import numpy as np


@dataclass
class AccountData:
    """账户数据结构"""
    total_asset: float = 0.0      # 总资产
    market_value: float = 0.0     # 总市值
    available: float = 0.0        # 可用资金
    daily_pnl: float = 0.0       # 当日盈亏

@dataclass
class PositionData:
    """持仓数据结构"""
    volume: int = 0              # 总持仓
    available: int = 0           # 可用持仓
    cost: float = 0.0           # 成本价
    current_price: float = 0.0   # 现价
    market_value: float = 0.0    # 市值
    pnl: float = 0.0            # 盈亏金额
    pnl_pct: float = 0.0        # 盈亏百分比
    daily_pnl: float = 0.0      # 当日盈亏
    daily_pnl_pct: float = 0.0  # 当日盈亏百分比
    name: str = ""              # 股票简称
    exchange: str = ""          # 交易所
    
@dataclass
class Decision:
    """投资决策数据结构"""
    symbol: str
    action: str  # "buy" or "sell"
    quantity: int
    price_range: Tuple[float, float]
    reason: str = ""  # 决策原因 

class DecisionEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, Decision):
            return obj.__dict__
        elif isinstance(obj, np.int64):
            return int(obj)
        return super().default(obj)

@dataclass
class AdviceData:
    """投资建议数据结构"""
    datetime: datetime
    advisor_name: str
    decisions: List[Decision]
    formatted_advice: str
    raw_data: Dict